const ts = require('typescript');
const JsLoader = require('./JsLoader');

function TsLoader(module, opt) {
    let encoding = opt.encoding || 'UTF-8';
    var result = ts.transpileModule(module.file.contents.toString(encoding), {
        reportDiagnostics: true,
        fileName: module.file.path,
        compilerOptions: {
            strict: true,
            strictFunctionTypes: true,
            strictPropertyInitialization: true,
            strictNullChecks: true,
            target: ts.ScriptTarget.Latest,
            jsx: ts.JsxEmit.Preserve
        }
    });
    if (result.diagnostics && result.diagnostics.length) {
        throw new Error('TsLoader Error:\n' + result.diagnostics.map(e => e.file.path + ':' + e.start + ':' + e.length + ': ' + e.messageText).join('\n'));
    }


    return JsLoader(module, opt, result.outputText);
}


module.exports = TsLoader;
